package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class GoodsTypeController {
    @Autowired
    private GoodsTypeService goodsTypeService;

    /**
     * 查询所有分类
     *
     * @return
     */
    @PostMapping("/goodsType/loadGoodsType")
    @ResponseBody
    public String loadGoodsType() {
        String result = goodsTypeService.getGoodsTypes();
        return result;
    }

    /**
     * 新增分类
     *
     * @return
     */
    @PostMapping("/goodsType/save")
    @ResponseBody
    public ServiceVO saveGoodsType(String goodsTypeName, Integer pId) {
        if (StringUtils.isEmpty(goodsTypeName) || pId == null || pId < 0) {
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
        goodsTypeService.saveGoodsType(goodsTypeName,pId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);

    }

    /**
     * 删除分类
     *
     * @return
     */
    @PostMapping("/goodsType/delete")
    @ResponseBody
    public ServiceVO removeGoodsType(Integer goodsTypeId) {
        if (goodsTypeId == null || goodsTypeId < 0) {
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
        goodsTypeService.removeGoodsType(goodsTypeId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }


}
